package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ActivityIgnoreImpl extends TempDao {

    public List<Integer> getActivityIgnoreList(long serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<Integer> list = new ArrayList<>();
        String sql = "select sai.activityId,serverList from t_s_system_activity_ignore sai " +
                " left join t_s_activity_main am on sai.activityId = am.activityId " +
                " where beginShowTime <= ? AND ? <= endShowTime AND state = 0";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setString(1, DateHelper.getCurrentDateTimeString());
            call.setString(2, DateHelper.getCurrentDateTimeString());
            rs = call.executeQuery();
            while (rs.next()) {
                List<Long> serverIdList = StringUtils.stringToLongList(rs.getString("serverList"),",");
                if(serverIdList.contains(serverId)){
                    int activityId = rs.getInt("activityId");
                    list.add(activityId);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
